package com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck;

import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck.vo.*;
import com.xiaoshuidi.cloud.module.member.convert.securitycheck.SecurityCheckRectificationItemConvert;
import com.xiaoshuidi.cloud.module.member.enums.SecurityCheckOverdueStatusEnum;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheck.SecurityCheckRectificationItemDO;
import com.xiaoshuidi.cloud.module.member.service.securitycheck.SecurityCheckRectificationItemService;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;

import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 安全检查整改项")
@RestController
@RequestMapping("/member/security-check-rectification-item")
@Validated
public class SecurityCheckRectificationItemController {

    @Resource
    private SecurityCheckRectificationItemService securityCheckRectificationItemService;

    @PostMapping("/create")
    @Operation(summary = "创建安全检查整改项")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:create')")
    public CommonResult<Long> createSecurityCheckRectificationItem(@Valid @RequestBody SecurityCheckRectificationItemCreateReqVO createReqVO) {
        return success(securityCheckRectificationItemService.createSecurityCheckRectificationItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安全检查整改项")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:update')")
    public CommonResult<Boolean> updateSecurityCheckRectificationItem(@Valid @RequestBody SecurityCheckRectificationItemUpdateReqVO updateReqVO) {
        securityCheckRectificationItemService.updateSecurityCheckRectificationItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安全检查整改项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:delete')")
    public CommonResult<Boolean> deleteSecurityCheckRectificationItem(@RequestParam("id") Long id) {
        securityCheckRectificationItemService.deleteSecurityCheckRectificationItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安全检查整改项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<SecurityCheckRectificationItemRespVO> getSecurityCheckRectificationItem(@RequestParam("id") Long id) {
        SecurityCheckRectificationItemDO securityCheckRectificationItem = securityCheckRectificationItemService.getSecurityCheckRectificationItem(id);
        return success(SecurityCheckRectificationItemConvert.INSTANCE.convert(securityCheckRectificationItem));
    }

    @GetMapping("/list")
    @Operation(summary = "获得安全检查整改项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<List<SecurityCheckRectificationItemRespVO>> getSecurityCheckRectificationItemList(@RequestParam("ids") Collection<Long> ids) {
        List<SecurityCheckRectificationItemDO> list = securityCheckRectificationItemService.getSecurityCheckRectificationItemList(ids);
        return success(SecurityCheckRectificationItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安全检查整改项分页")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<PageResult<SecurityCheckRectificationItemRespVO>> getSecurityCheckRectificationItemPage(@Valid SecurityCheckRectificationItemPageReqVO pageVO) {
        return success(securityCheckRectificationItemService.getSecurityCheckRectificationItemPage(pageVO.getPageNo(), pageVO.getPageSize(), pageVO.getSecurityCheckId()));
    }

    @GetMapping("/get-hazard-photo")
    @Operation(summary = "获得整改项隐患图片")
    @Parameter(name = "id", description = "整改项id")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<List<String>> getHazardPhoto(Long id) {
        return success(securityCheckRectificationItemService.getHazardPhoto(id));
    }

    @GetMapping("/get-return-photo")
    @Operation(summary = "获得整改项回复图片")
    @Parameter(name = "id", description = "整改项id")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<List<String>> getReturnPhoto(Long id) {
        return success(securityCheckRectificationItemService.getReturnPhoto(id));
    }

    @GetMapping("/check-for-expiring-corrections")
    @Operation(summary = "检查是否存在即将到期/已到期未处理的整改项")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<Integer> CheckForExpiringCorrections() {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return success(securityCheckRectificationItemService.CheckForExpiringCorrections(loginUserId));
    }

    @GetMapping("/hazard-ledger")
    @Operation(summary = "隐患台账")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<PageResult<HazardLedgerPageRespVO>> hazardLedger(HazardLedgerPageReqVO reqVO) {
        return success(securityCheckRectificationItemService.hazardLedger(reqVO));
    }

    @GetMapping("/hazard-ledger-export")
    @Operation(summary = "隐患台账导出")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:export')")
    @OperateLog(type = EXPORT)
    public void hazardLedgerExport(HazardLedgerExportReqVO reqVO, HttpServletResponse response) throws IOException {
        List<HazardLedgerPageRespVO> hazardLedgerPageRespVOS = securityCheckRectificationItemService.hazardLedgerExport(reqVO);
        List<HazardLedgerExportVO> datas = SecurityCheckRectificationItemConvert.INSTANCE.convert(hazardLedgerPageRespVOS);
        for (int i = 0; i < datas.size(); i++) {
            datas.get(i).setNo(i);
            datas.get(i).setIsOverdueStr(SecurityCheckOverdueStatusEnum.getNameByValue(datas.get(i).getIsOverdue()));
        }
        ExcelUtils.write(response, "隐患台账.xls", "数据", HazardLedgerExportVO.class, datas);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安全检查整改项 Excel")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:export')")
    @OperateLog(type = EXPORT)
    public void exportSecurityCheckRectificationItemExcel(@Valid SecurityCheckRectificationItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SecurityCheckRectificationItemDO> list = securityCheckRectificationItemService.getSecurityCheckRectificationItemList(exportReqVO);
        // 导出 Excel
        List<SecurityCheckRectificationItemExcelVO> datas = SecurityCheckRectificationItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "安全检查整改项.xls", "数据", SecurityCheckRectificationItemExcelVO.class, datas);
    }

    @GetMapping("/rectification-item-by-user")
    @Operation(summary = "当前登录用户负责的整改项")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<PageResult<HazardLedgerPageRespVO>> rectificationItemByUser(HazardLedgerPageReqVO reqVO) {
        reqVO.setUserId(SecurityFrameworkUtils.getLoginUserId());
        return success(securityCheckRectificationItemService.rectificationItemByUser(reqVO));
    }

    @GetMapping("/rectification-item-detail")
    @Operation(summary = "整改项详情")
    @Parameter(name = "id", description = "整改项id")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:query')")
    public CommonResult<RectificationItemDetailRespVO> rectificationItemDetail(Long id) {
        return success(securityCheckRectificationItemService.rectificationItemDetail(id));
    }

    @PostMapping("/rectification-item-reply")
    @Operation(summary = "整改项回复")
    @PreAuthorize("@ss.hasPermission('member:security-check-rectification-item:update')")
    public CommonResult<Boolean> rectificationItemReply(@RequestBody @Valid RectificationItemReplyReqVO reqVO) {
        return success(securityCheckRectificationItemService.rectificationItemReply(reqVO));
    }


}
